﻿using CoachingOrganizer.iOS.Controllers.Tools.MoneyCalculation;
using CoachingOrganizer.iOS.Decorators;
using CoachingOrganizer.iOS.Helpers;
using CoachingOrganizer.Logic.Models.Tools.MoneyCalculation;
using MonoTouch.UIKit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CoachingOrganizer.iOS.DataSources.Tools.MoneyCalculation
{
    public class OccasionalSpentsDataSource : UITableViewSource
    {
        public const string CellIdentifer = "OccasionalSpentsItemCell";

        private readonly UITableView _view;
        private readonly OccasionalSpentsMoneyCalculationDetailsViewController _controller;
        private readonly MoneyCalculationModel _model;
        private readonly SimpleTableDataSourceDecorator<OccasionalSpentsItemViewController, OccasionalMoneyFlowItemModel> _tableCellsSource;

        public OccasionalSpentsDataSource(UITableView view, OccasionalSpentsMoneyCalculationDetailsViewController controller, MoneyCalculationModel model)
        {
            _view = view;
            _controller = controller;
            _model = model;
            _tableCellsSource = new SimpleTableDataSourceDecorator<OccasionalSpentsItemViewController, OccasionalMoneyFlowItemModel>(CellIdentifer, _view, _controller, p => _model.Spents.OccasionalItems.ElementAtOrDefault(p.Row), m => new OccasionalSpentsItemViewController(m));
            model.Spents.OccasionalMoneyFlowItemsCollectionChanged += Spents_OccasionalMoneyFlowItemsCollectionChanged;
        }

        public override UITableViewCell GetCell(UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
        {
            return _tableCellsSource.GetCell(indexPath);
        }

        public override int RowsInSection(UITableView tableview, int section)
        {
            return UIHelper.GetCollectionItemsCount( _model.Spents.OccasionalItems);
        }

        private void Spents_OccasionalMoneyFlowItemsCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            _tableCellsSource.ProceedCollectionChanged(e);
        }
    }
}
